<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Controller;
use think\Request;

class BatteryOrder extends Admin
{
	public function index()
	{
		$where = [];
		// $where[] = ['id','=',session('login')['fwgl_id']];
		$with = 'user';
        if($this->request->isPost())
        {
            $post = post();
            if(isset($post['name'])) $where[] = ['name','like','%' . $post['name'] . '%'];
            if(isset($post['status'])) $where[] = ['status','=',$post['status']];
            if(isset($post['orderid'])) $where[] = ['orderid','like',$post['orderid'] . '%'];
            if(isset($post['time'])){
            	list($first,$last) = explode('至',$post['time']);
                $where[] = ['create_time','>=',trim($first)];
                $where[] = ['update_time','<=',trim($last)];
            }
            if(isset($post['user'])){
            	$user = model('user')->field('id')->where('name','like',$post['user'] . '%')->select();
            	if(!empty($user)){
            		$user = $user->toArray();
            		$id = [];
            		foreach($user as $v)
            		{
            			if(!in_array($v['id'],$id)){
            				$id[] = $v['id'];
            			}
            		}
            		$id = implode(',',$id);
            		$where[] = ['user_id','in',$id];
            	}
            }
            $list = ysh_select($this->request->controller(),['where'=>$where,'with'=>$with]);
        }else{
            $list = ysh_select($this->request->controller(),['with'=>$with]);
        }
        // dump($list);
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
	}
	public function view()
	{
		$id = input('id');
		$list = model($this->request->controller())->where('id','=',$id)->with('battery.batteryba')->find()->toArray();
		if($list['status'] == 1){
			$sc = time() - $list['create_time'];
		}else{
			$sc = $list['update_time'] - $list['create_time'];
		}
		//小时
		$hous = floor($sc/60/60);
		//分钟
		$fzm = $sc-$hous*60*60;
		$fz = floor($fzm/60);
		//秒
		$miao = $fzm-$fz*60;
		$list['sc'] = $hous.'时'.$fz.'分'.$miao.'秒';
		$this->assign('list',$list);
		return $this->fetch();
	}
	public function deletes()
	{
		$id = input('id');
		$order = model($this->request->controller())->get($id)->toArray();
		model($this->request->controller())->destroy($id);
		if($order['status'] == 1){
			model('battery')->save(['stauts'=>2,'user_id'=>0],['id'=>$order['battery_id']]);
		}
		return $this->redirect('index');
	}
}
